package com.unlcn.ils.kas.controller;

import com.unlcn.ils.kas.dao.model.sysadmin.Permission;
import com.unlcn.ils.kas.vo.RestVO;
import com.unlcn.ils.kas.service.sysadmin.PermissionService;
import com.unlcn.ils.kas.vo.ElTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoguixin on 2017/12/12.
 */
@Controller
@RequestMapping(value = "/permission", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping(value = "/firstMenu")
    @ResponseBody
    public RestVO<List<Map<String, Object>>> getFirstDirectory() {

        RestVO<List<Map<String, Object>>> result = new RestVO<>(0, null, "success");

        List<Permission> firstPermissons = permissionService.getFistPermission();
        if (firstPermissons == null || firstPermissons.size() > 0) {
            List<Map<String, Object>> mapList = new ArrayList<>();
            firstPermissons.stream().forEach(permission -> {
                Map<String, Object> mapPermisson = new HashMap<String, Object>();
                mapPermisson.put("id", permission.getId().toString());
                mapPermisson.put("name", permission.getName());
                mapPermisson.put("orders", permission.getOrders());
                mapList.add(mapPermisson);
            });
            result.setData(mapList);
            return result;
        } else {
            result.setCode(100);
            result.setMessage("权限为空");
            return result;
        }
    }

    @GetMapping(value = "/subMenu")
    @ResponseBody
    public RestVO<List<Map<String, String>>> getSubMenu(@RequestParam("pid") Integer pid) {
        RestVO<List<Map<String, String>>> result = new RestVO<>(0, null, "success");

        List<Permission> permissionList = permissionService.listChildPermission(pid);
        List<Map<String, String>> permMapList = new ArrayList<>();
        if (permissionList != null && permissionList.size() > 0) {
            permissionList.stream().forEach(permission -> {
                Map<String, String> nameMap = new HashMap<>();
                nameMap.put("name", permission.getName());
                permMapList.add(nameMap);
            });
            result.setData(permMapList);
            return result;
        } else {
            result.setCode(-1);
            result.setMessage("子菜单为空");
            return result;
        }
    }

    @GetMapping(value = "/treeAll")
    @ResponseBody
    public RestVO<List<ElTreeVO>> getSubMenu() {
        RestVO<List<ElTreeVO>> responseBO = new RestVO<>();
        List<Permission> listAllPermission = permissionService.listAllPermission();
        ElTreeVO elTreeVO = new ElTreeVO();
        elTreeVO.setId(0);
        elTreeVO.setLabel("根权限");
        buildElTree(elTreeVO, listAllPermission);
        responseBO.setData(elTreeVO.getChildren());
        return responseBO;
    }

    private void buildElTree(ElTreeVO elTreeVO, List<Permission> listAllPermission) {
        List<ElTreeVO> children = new ArrayList<>();
        for (Permission permission : listAllPermission) {
            if (permission.getPid() != null && permission.getPid().equals(elTreeVO.getId())) {
                ElTreeVO childElTree = new ElTreeVO();
                childElTree.setId(permission.getId());
                childElTree.setLabel(permission.getName());
                buildElTree(childElTree, listAllPermission);
                children.add(childElTree);
            }

            elTreeVO.setChildren(children);
        }
    }
}
